home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / st80_pr4.lha / st80_pre4 / latex.st (.txt) < prev   
LaTeX Document  |  1993-07-24  |  12KB  |  334 lines

  1. "       NAME            latex.st
  2.         AUTHOR          mcconnel@cs.uiuc.edu
  3.         FUNCTION    print out ST source as LaTeX source
  4.         ST-VERSIONS     2.5, 4.0
  5.         PREREQUISITES   
  6.         CONFLICTS       
  7.         DISTRIBUTION    world
  8.         VERSION         1.0
  9.         DATE        June 21, 1991
  10. SUMMARY latex.st
  11. This code causes the print-out menu item in the various browser panes
  12. to produce LaTeX.  Printing out categories and classes yields
  13. self-contained LaTeX documents; printing out protocols and methods
  14. yields LaTeX output suitable for inclusion in a LaTeX document.  In
  15. particular, printing a category yields a document with a table of
  16. contents (showing what page the class definitions are on), and so you
  17. will need to LaTeX the resulting file twice.
  18.         Carl McConnell
  19. !Browser methodsFor: 'private-category functions'!
  20. printOutCategory 
  21.     organization printOutCategory: category! !
  22. !Browser methodsFor: 'private-class functions'!
  23. printOutClass 
  24.     self selectedClass printOut! !
  25. !Browser methodsFor: 'private-protocol functions'!
  26. printOutProtocol 
  27.     self selectedClass printOutCategory: protocol! !
  28. !Browser methodsFor: 'private-selector functions'!
  29. printOutMessage 
  30.     self selectedClass printOutMessage: selector! !
  31. !SystemOrganizer methodsFor: 'fileIn/Out'!
  32. printOutCategory: category 
  33.     LatexStream for: category do: 
  34.         [:fileStream | 
  35.         Transcript cr; show: 'Printing out ' , category , ':'; cr.
  36.         fileStream beginCategory: category.
  37.         self printOutCategory: category on: fileStream.
  38.         fileStream endCategory]!
  39. printOutCategory: category on: aFileStream 
  40.     | firstTrip |
  41.     firstTrip _ true.
  42.     (self superclassOrder: category)
  43.         do: 
  44.             [:class | 
  45.             "The test prevents a blank page from being printed right after 
  46.              the table of contents."
  47.             firstTrip
  48.                 ifTrue: [firstTrip _ false]
  49.                 ifFalse: [aFileStream anotherClass].
  50.             Transcript tab; show: class name; cr.
  51.             class printOutOn: aFileStream]! !
  52. !Filename methodsFor: 'stream creation'!
  53. latexStream
  54.     "Answer a write stream connected to the file represented by
  55.     the receiver."
  56.     ^LatexStream on: (FileConnection
  57.             openFileNamed: self
  58.             mode: #writeOnly
  59.             creationRule: #truncateOrCreate)! !
  60. ExternalWriteStream subclass: #LatexStream
  61.     instanceVariableNames: 'inComment lastCharacter generateTableOfContents '
  62.     classVariableNames: 'CharacterMap '
  63.     poolDictionaries: ''
  64.     category: 'OS-Streaming'!
  65. LatexStream comment:
  66. 'I translate Smalltalk into LaTeX.
  67. Problems: tables in comments aren''t always correctly aligned.
  68. Author:
  69.     Carl McConnell
  70.     mcconnel@cs.uiuc.edu
  71.     Department of Computer Science
  72.     University of Illinois at Urbana-Champaign
  73.     5/90'!
  74. !LatexStream methodsFor: 'beginning-ending'!
  75. beginCategory: category 
  76.     self generateTableOfContents: true.
  77.     self beginDocument.
  78.     self line: '\title{{\bf ', category, '}}'.
  79.     self line: '\author{', self version, '}'.
  80.     self line: '\date{' , Date today printString , ' ' , Time now printString , '}'.
  81.     self line: '\maketitle'.
  82.     self line: '\pagenumbering{roman}'.
  83.     self line: '\setcounter{page}{1}'.
  84.     self line: '\tableofcontents'.
  85.     self line: '\clearpage'.
  86.     self line: '\pagenumbering{arabic}'.
  87.     self line: '\setcounter{page}{1}'.
  88.     self line: '%'!
  89. beginClass
  90.     self beginDocument.
  91.     self line: '\begin{center}'.
  92.     self line: '{\tiny ', self version, '}'.
  93.     self line: '\end{center}'.
  94.     self blankLines: 3!
  95. endCategory
  96.     self endDocument!
  97. endClass
  98.     self endDocument! !
  99. !LatexStream methodsFor: 'printing'!
  100. printClassComment: aString 
  101.     aString isEmpty ifTrue: [^self].
  102.     self blankLines: 1.
  103.     self beginFont: '\it'.
  104.     self printSmalltalkString: aString.
  105.     self endFont; cr!
  106. printClassDefinitionFor: aString from: aTable 
  107.     self line: '\begin{center}'.
  108.     self beginFont: '\Large \bf'.
  109.     self nextPutAll: aString.
  110.     self endFont; cr.
  111.     self line: '\end{center}'.
  112.     self blankLines: 3.
  113.     self line: '\markright{{\bf ' , aString , '} \hfill}'.
  114.     self generateTableOfContents ifTrue: [self line: '\addcontentsline{toc}{section}{' , aString , '}'].
  115.     self line: '\begin{tabular}{ll}'.
  116.     aTable do: 
  117.         [:assoc | 
  118.         self nextPutAll: assoc key.
  119.         self nextPutAll: '    & '.
  120.         assoc value isEmpty
  121.             ifTrue: [self beginFont: '\it'; nextPutAll: 'none'; endFont]
  122.             ifFalse: [self nextPutAll: '\parbox[t]{4.0in}{\raggedright '; beginFont: '\bf'; nextPutAll: assoc value; endFont; nextPut: $}].
  123.         assoc = aTable last
  124.             ifTrue: [self cr]
  125.             ifFalse: [self line: ' \\']].
  126.     self line: '\end{tabular}'!
  127. printMethod: aSelector withBody: aString 
  128.     self beginFont: '\bf'; printSmalltalkString: aSelector; endFont.
  129.     self cr; line: '\nopagebreak'.
  130.     self printSmalltalkString: aString.
  131.     self cr!
  132. printProtocolHeader: aString 
  133.     self beginFont: '\sl'.
  134.     self nextPutAll: 'Protocol for '; nextPutAll: aString.
  135.     self endFont; cr.
  136.     self line: '\nopagebreak'! !
  137. !LatexStream methodsFor: 'separating'!
  138. anotherClass
  139.     self line: '%'.
  140.     self line: '\clearpage'!
  141. anotherMethod
  142.     self blankLines: 2!
  143. anotherProtocol
  144.     self blankLines: 3! !
  145. !LatexStream methodsFor: 'private miscellaneous'!
  146. blankLines: n 
  147.     self cr.
  148.     self nextPutAll: '\addvspace{'; print: n; nextPutAll: 'ex}'; cr.
  149.     self line: '\noindent'!
  150. generateTableOfContents
  151.     ^generateTableOfContents isNil ifTrue: [false] ifFalse: [generateTableOfContents]!
  152. generateTableOfContents: aBoolean
  153.     generateTableOfContents _ aBoolean!
  154. line: aString
  155.     self nextPutAll: aString; cr!
  156. version
  157.     "The version name for Objectworks 4.0
  158.     contains a backslash, hence this hack."
  159.     ^Smalltalk version copyReplaceAll: '\' with: '$\backslash$'! !
  160. !LatexStream methodsFor: 'private printing'!
  161. printSmalltalkString: aString 
  162.     | output |
  163.     inComment _ false.
  164.     lastCharacter _ nil.
  165.     aString do: 
  166.         [:aCharacter | 
  167.         output _ CharacterMap at: aCharacter asciiValue.
  168.         output notNil
  169.             ifTrue: [self nextPutAll: output]
  170.             ifFalse: ["This character must require special handling."
  171.                 self printSpecialCharacter: aCharacter].
  172.         lastCharacter _ aCharacter]!
  173. printSpecialCharacter: aCharacter 
  174.     aCharacter = $"
  175.         ifTrue: 
  176.             [inComment
  177.                 ifTrue: [self nextPutAll: ''''''; endFont]
  178.                 ifFalse: [self beginFont: '\it'; nextPutAll: '``'].
  179.             inComment _ inComment not.
  180.             ^self].
  181.     aCharacter = $=
  182.         ifTrue: 
  183.             [self nextPutAll: (lastCharacter = $:
  184.                     ifTrue: ['=']
  185.                     ifFalse: ['$=$']).
  186.             ^self]! !
  187. !LatexStream methodsFor: 'private beginning-ending'!
  188. beginDocument
  189.     self line: '\documentstyle[11pt]{report}'.
  190.     self line: '\oddsidemargin 0in \evensidemargin 0in'.
  191.     self line: '\marginparwidth 0in \marginparsep 0in'.
  192.     self line: '\topmargin 0in \topskip 0in'.
  193.     self line: '\headheight 0.25in \headsep 0.25in'.
  194.     self line: '\footheight 0in \footskip 0.25in'.
  195.     self line: '\textheight 8.25in \textwidth 6.5in'.
  196.     self line: '\raggedbottom \sloppy'.
  197.     self line: '\pagestyle{myheadings}'.
  198.     self line: '%'.
  199.     self line: '\begin{document}'!
  200. beginFont: aString
  201.     self nextPut: ${.
  202.     self nextPutAll: aString; space!
  203. endDocument
  204.     self line: '\end{document}'!
  205. endFont
  206.     self nextPut: $}! !
  207. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  208. LatexStream class
  209.     instanceVariableNames: ''!
  210. !LatexStream class methodsFor: 'class initialization'!
  211. at: aCharacter put: anObject
  212.     CharacterMap at: aCharacter asciiValue put: anObject.
  213.     ^anObject!
  214. createCharacterMap
  215.     CharacterMap _ Array new: 127.
  216.     1 to: 127 do: [:i | CharacterMap at: i put: (String with: (Character value: i))]!
  217. initialize
  218.     "Initialize the table for mapping characters to LaTeX. Characters 
  219.     for which special (or no) action is to be taken have nil entries. We 
  220.     won't have to handle NUL characters, so the table size is 127."
  221.     | lineEndString first |
  222.     self createCharacterMap.
  223.     "These require special handling."
  224.     self at: $" put: nil.
  225.     self at: $= put: nil.
  226.     "We assume that we can distinguish the end-of-line string by its 
  227.     first character; the rest we just ignore."
  228.     lineEndString _ self lineEndString.
  229.     first _ true.
  230.     lineEndString do: [:ch | self at: ch put: (first
  231.                 ifTrue: 
  232.                     [first _ false.
  233.                     '\\' , lineEndString]
  234.                 ifFalse: [nil])].
  235.     self at: Character tab put: '\hspace*{4ex}'.
  236.     self at: $# put: '\#'.
  237.     self at: $$ put: '\$'.
  238.     self at: $% put: '\%'.
  239.     self at: $& put: '\&'.
  240.     self at: $_ put: '$\leftarrow$'.
  241.     self at: ${ put: '\{'.
  242.     self at: $} put: '\}'.
  243.     self at: $~ put: '{\char126}'.
  244.     self at: $^ put: '$\uparrow$'.
  245.     self at: $\ put: '$\backslash$'.
  246.     self at: $| put: '$|$'.
  247.     self at: $> put: '$>$'.
  248.     self at: $< put: '$<$'.
  249.     self at: $+ put: '$+$'.
  250.     self at: $- put: '$-$'!
  251. lineEndString
  252.     "Return the end-of-line string."
  253.     | stream |
  254.     stream _ WriteStream on: (String new: 4).
  255.     stream cr.
  256.     ^stream contents! !
  257. !LatexStream class methodsFor: 'instance creation'!
  258. for: aString do: aBlock 
  259.     | fileName fileStream |
  260.     fileName _ Filename
  261.                 request: 'Print LaTeX on'
  262.                 initially: aString , '.tex'
  263.                 shouldExist: nil.
  264.     fileName = '' ifTrue: [^nil].
  265.     fileStream _ (Filename named: fileName) latexStream.
  266.     [aBlock value: fileStream]
  267.         valueNowOrOnUnwindDo: [fileStream close]! !
  268. LatexStream initialize!
  269. !ClassDescription methodsFor: 'fileIn/Out'!
  270. definitionAsTable
  271.     | table |
  272.     table _ OrderedCollection new.
  273.     table add: (Association key: 'class name' value: self name).
  274.     table add: (Association key: 'superclass' value: (superclass == nil
  275.                 ifTrue: ['']
  276.                 ifFalse: [superclass name])).
  277.     table add: (Association key: 'instance variable names' value: self instanceVariablesString).
  278.     table add: (Association key: 'class variable names' value: self classVariablesString).
  279.     table add: (Association key: 'pool dictionaries' value: self sharedPoolsString).
  280.     table add: (Association key: 'category' value: self category asString).
  281.     ^table!
  282. printOutCategory: aString 
  283.     LatexStream for: self name , '-' , aString do: [:fileStream | self printOutCategory: aString on: fileStream]!
  284. printOutCategory: aString on: aFileStream 
  285.     self printOutCategoryChunk: aString on: aFileStream.
  286.     (self organization listAtCategoryNamed: aString)
  287.         do: 
  288.             [:sel | 
  289.             aFileStream anotherMethod.
  290.             self printOutMethodChunk: sel on: aFileStream]!
  291. printOutCategoryChunk: aString on: aFileStream 
  292.     aFileStream printProtocolHeader: aString!
  293. printOutMessage: aString 
  294.     LatexStream for: self name , '-' , aString do: [:fileStream | self printOutMethodChunk: aString on: fileStream]!
  295. printOutMethodChunk: aSelector on: aFileStream 
  296.     | string parser lastSelectorChar |
  297.     Cursor write
  298.         showWhile: 
  299.             [string _ self sourceCodeAt: aSelector.
  300.             (parser _ self parserClass new) parseSelector: string.
  301.             lastSelectorChar _ parser endOfLastToken min: string size.
  302.             ["The parser includes the line separator(s) as part of the selector. 
  303.             Although I don't think it makes any difference in the printed output, I prefer 
  304.             the line separator(s) to be part of the body, since it makes the LaTeX
  305.             look nicer; hence the loop."
  306.             (string at: lastSelectorChar) tokenish]
  307.                 whileFalse: [lastSelectorChar _ lastSelectorChar - 1].
  308.             aFileStream printMethod: (string copyFrom: 1 to: lastSelectorChar)
  309.                 withBody: (string copyFrom: lastSelectorChar + 1 to: string size)]!
  310. printOutOn: aFileStream 
  311.     aFileStream printClassDefinitionFor: self name from: self definitionAsTable.
  312.     aFileStream printClassComment: self comment.
  313.     self organization categories do: 
  314.         [:heading | 
  315.         aFileStream anotherProtocol.
  316.         self printOutCategory: heading on: aFileStream]! !
  317. !Metaclass methodsFor: 'accessing'!
  318. category
  319.     ^thisClass category! !
  320. !Class methodsFor: 'fileIn-Out'!
  321. printOut
  322.     LatexStream for: self name do: 
  323.         [:fileStream | 
  324.         Transcript cr; show: 'Printing out ' , self name.
  325.         fileStream beginClass.
  326.         self printOutOn: fileStream.
  327.         fileStream endClass]!
  328. printOutOn: aFileStream 
  329.     super printOutOn: aFileStream.
  330.     self class nonTrivial
  331.         ifTrue: 
  332.             [aFileStream anotherClass.
  333.             self class printOutOn: aFileStream]! !
  334.